package com.edu.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.tomcat.util.buf.UDecoder;

import com.sun.net.httpserver.HttpServer;

import jdk.internal.org.objectweb.asm.util.CheckAnnotationAdapter;

import com.edu.Sevice.UserService;
import com.edu.model.User;;

@WebServlet("/user")
public class UserServlet extends HttpServlet{
	UserService us = new UserService();
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取方法名称
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html,utf-8");
		String method = request.getParameter("method");
		if(method.equals("regist")) {
			regist(request,response);
		}else if(method.equals("login")) {
			login(request,response);
		}else if(method.equals("logout")) {
			logout(request,response);
		}else if("check_user".equals(method)) {
			check_user(request,response);
		}
	}

	

	private void check_user(HttpServletRequest request, HttpServletResponse response) throws IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String username = request.getParameter("username");
		PrintWriter writer = response.getWriter();
		if(us.check(username)) {
			writer.write("{\"code\":0,\"Message\":\"\"}");
		}else {
			//"code":1,"Message":"用户名已存在"
			writer.write("{\"code\":1,\"Message\":\"用户名已存在\"}");
		}
			
		
	}


	private void logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
		HttpSession session = request.getSession();
		session.invalidate();
		String path = request.getContextPath();
		response.sendRedirect(path + "/index.jsp");
	}

	private void regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//从前台获取信息
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String sex = request.getParameter("sex");
		String phone = request.getParameter("phone");
		String email =request.getParameter("email");
		//将信息存入实体类中
		//System.out.println("------"+username+"------"+sex+"------"+password+"------"+phone+"------"+email);
		User user = new User(username,sex,password,phone,email);
		Boolean bool =us.regist(user);
		String path = request.getContextPath();  
		if(bool){
		request.getRequestDispatcher("/login.jsp").forward(request, response);
		}else {
			request.getRequestDispatcher("/regist.jsp").forward(request, response);
		}
	}
	
	private void login(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		User user = us.login(username, password);
		if(user != null) {
			System.out.println("数据库中有该用户");
			HttpSession session = request.getSession();
			session.setAttribute("user", user);
			String path = request.getContextPath();  //?
			response.sendRedirect(path + "/index.jsp");
		}else {
			System.out.print("数据库中没有该用户，请注册");
			request.setAttribute("username", username);
			request.setAttribute("password", password);
			request.setAttribute("errormsg", "用户名或密码错误");
			request.getRequestDispatcher("/regist.jsp").forward(request, response);
		}
	}
}
